package com.Util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.exception.LMSException;

/**
 * The Class LMSUtil.
 * 
 * @author : gbs02399 Created on : Jul 6, 2012 Project : LMS File Name :
 *         LMSUtil.java
 */
public class LMSUtil {

	/**
	 * Validate date.
	 * 
	 * @param fromDate
	 *            the from date
	 * @param toDate
	 *            the to date
	 * @throws LMSException
	 *             the lMS exception
	 */
	public static void validateDate(final String fromDate, final String toDate)
			throws LMSException {
		final Date startDate = getDate(fromDate);
		final Date endDate = getDate(toDate);
		if (startDate.getTime() > endDate.getTime()) {
			throw new LMSException("Startdate should be Lesser than endDate");
		}
	}

	/**
	 * Gets the date.
	 * 
	 * @param dateInString
	 *            the date in string
	 * @return the date
	 * @throws LMSException
	 *             the lMS exception
	 */
	public static Date getDate(final String dateInString) throws LMSException {
		if (dateInString == null) {
			throw new LMSException("DATE NOT VALID");
		}
		Date date = null;
		final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		sdf.setLenient(false);
		try {
			date = sdf.parse(dateInString);
			return date;
		} catch (final ParseException e) {
			throw new LMSException("DATE NOT VALID");
		}
	}

	/**
	 * Gets the datein string.
	 * 
	 * @param date
	 *            the date
	 * @return the datein string
	 * @throws LMSException
	 *             the lMS exception
	 */
	public static String getDateinString(final Date date) throws LMSException {
		final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		return dateFormat.format(date);
	}
}
